package org.kukker.list;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import org.kukker.event.Event;
import org.kukker.list.view.ListItemViewFactory;
import org.kukker.list.view.ListItemViewFactoryException;
import org.kukker.upcoming.UpcomingEvent;
import org.kukker.upcoming.UpcomingEventView;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.database.DataSetObserver;
import android.text.util.Linkify;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnPopulateContextMenuListener;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsoluteLayout;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.xml.dom.ElementImpl;

public class KukkerListAdapter extends BaseAdapter {
	Event[] mEvents;
	private String nsURI = "org.kukker.ui.extra.data";
	private final String collapseAttrName = "isCollapsed";
	private Context mContext;
	private KukkerListView mListView;
	private int mDataCount;
	
	public void setListView(KukkerListView listView){
		this.mListView = listView;
	}
	/*
	public ListView getListView(){
		return this.mListView;
	}
	*/
		
	public void setData(Event events[]) {
		if(events != null){
			this.mEvents = events;
			this.mDataCount = events.length;
			this.notifyDataSetChanged();
		}else{
			this.mDataCount = 0;
			this.mEvents = null;
			this.notifyDataSetChanged();
		}
	}

	public KukkerListAdapter(Context context) {

		mContext = context;
	}

	public int getCount() {
		return this.mDataCount;
		
	
	}
	/*
	private OnPopulateContextMenuListener  mOnPopulateContextMenuListener; 
	public void setListItemOnPopulateContextMenuListener(OnPopulateContextMenuListener listener){
		this.mOnPopulateContextMenuListener = listener;
	}
	*/

	public Object getItem(int position) {
		
		if(position >= this.mDataCount || position == -1){
			return null;
		}else{
			return this.mEvents[position];
		}
		/*
		if (mEvents == null) {
			return null;
		}
		return mEvents[position];
		*/
	}

	public long getItemId(int position) {
		// Log.i("DEBUG","position: "+position);
		return position;
	}

	/**
	 * Make a EventView to hold each row.
	 * 
	 * @see android.widget.ListAdapter#getView(int, android.view.View,
	 *      android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.i("EventListAdapter", "GetView() , Position:" + position +", NumData: "+this.mEvents.length);
		
		
		if(position >= this.mDataCount || position < 0){
			//Log.i("EventListAdapter", "GetView() returning null");
			return null;
		}
		
		// get one data at position
		Event data;
		try{
			data = mEvents[position];
		}catch(Exception e){
			return null;
		}
		 
//		Log.i("viewcreation","context class:"+this.mContext.getClass().getName());
	//	Log.i("viewcreation","data class:"+data.getClass().getName());
		
		if (data != null) {
			
			//Log.i("debug","origin:"+data.originId+", type:"+data.typeId);
		
				//without using reflection			
				if(data.typeId == 1 &&	data.originId == 0){				
					KukkerListItemView v = new KukkerListItemView(this.mContext, data,this.mListView);
					v.setListView(this.mListView);
					v.setPosition(position);
					return v;
					
				}
				else if(data.typeId == 1 && data.originId == 1){
					UpcomingEventView v = new UpcomingEventView(this.mContext,(UpcomingEvent)data,this.mListView);
					v.setListView(this.mListView);
					v.setPosition(position);
					return v;
				}
				
		
				
				/*
				//USING REFLECTION				
				{
				// get the appropriate view class for this data
				Class viewClass = ListItemViewFactory.getCreator(data);
				Class dataClass = data.getClass();
				
		//		Log.i("viewcreation","view class:"+viewClass.getName());
				
				Constructor con = viewClass.getConstructor(				
						new Class[] {Context.class, dataClass});
		
			//	Log.i("viewcreation","constructor:"+con.getClass().getName());
				
				// create the list item view for this data
				KukkerListItemView aView = (KukkerListItemView) con.newInstance(this.mContext, data);
				aView.setListView(this.mListView);
				aView.setPosition(position); //important to set the position
				return aView;
				}
				*/
				//Log.i("EventListAdapter", "GetView() returning not null");
				

			 
			/*catch (NoSuchMethodException e) {

				Log.e("error",this.getClass().getName()+", cause:"+e.getCause()+", message:"+e.getMessage());
				Log.i("viewcreation", "error: NoSuchMethodException" + e.getMessage());
			} catch (ListItemViewFactoryException e) {
				Log.e("error",this.getClass().getName()+", cause:"+e.getCause()+", message:"+e.getMessage());
				Log.i("viewcreation", "error: ListItemViewFactoryException, " + e.getMessage());
			} catch (IllegalArgumentException e) {
				Log.e("error",this.getClass().getName()+", cause:"+e.getCause()+", message:"+e.getMessage());
				Log.i("viewcreation", "error: IllegalArgumentException, " + e.getMessage());
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				Log.e("error",this.getClass().getName()+", cause:"+e.getCause()+", message:"+e.getMessage());
				Log.i("viewcreation", "error: InstantiationException, " + e.getMessage());
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				Log.e("error",this.getClass().getName()+", cause:"+e.getCause()+", message:"+e.getMessage());
				Log.i("viewcreation", "error: llegalAccessException, " + e.getMessage());
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				Log.e("error",this.getClass().getName()+", cause:"+e.getCause()+", message:"+e.getMessage());
				Log.i("viewcreation", "error: InvocationTargetException, " + e.getMessage()+", "+e.getCause());
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		}
		return null;
	}

	

} // end of class
